/**
 * @file {piantiel 工站的flaw_filter model}
 */
 'use strict'

 const { DataTypes, Model, Op } = require('../../../azeroth').sequelize;
 const {flawFilterInitData} = require('../common/init_data');
 class FlawFilterPiantie extends Model { }

 /**
  * @desc 映射需要的字段 【源库的 部分字段 --> 目标库字段】
  */
  const initFlawFilterData = (sequelize_aim) => {
    FlawFilterPiantie.init(
         ...flawFilterInitData,
         {
             freezeTableName: true,
             sequelize: sequelize_aim,
             modelName: 'flaw_filter',
             timestamps: false
         }
    )
  }
  FlawFilterPiantie.getMaxDoffUid = async (sequelize_source) => {
    let sql = 'select max(doffId) as panel_uid from flaw';
    let res = await sequelize_source.query(sql, { type: sequelize_source.QueryTypes.SELECT });
    return res[0].panel_uid;
  }
  /**
   *
   * @param {*} sequelize_aim
   * @param {*} flawFilterUid
   * @returns
   */
  FlawFilterPiantie.getLatestFlawFilterId = async (sequelize_aim, flawFilterUid) => {
    let sql = null;
    // 第一次进的时候，这里FlawFIlter 也对应更新上一次可能没同步完的数据
    if (flawFilterUid) {
        sql = 'select min(id) as id from flaw_filter where panel_table_uid = ' + flawFilterUid;
        let res = await sequelize_aim.query(sql, { type: sequelize_aim.QueryTypes.SELECT });
        return res[0].id ? res[0].id - 1 : 0;
    } else {
        sql = 'select max(id) as id from flaw_filter';
        let res = await sequelize_aim.query(sql, { type: sequelize_aim.QueryTypes.SELECT });
        return res[0].id ? res[0].id : 0;
    }
  }
  /**
   * 统计缺陷个数panel信息等
   * @param {*} sequelize_source
   * @param {*} uid
   */
  FlawFilterPiantie.getFlawCountByPanel = async (sequelize_source, queryStr) => {
    let query = typeof(queryStr) == 'string' ? `b.panel_id = "${queryStr}"` : `a.uid = ${queryStr}`;
    let sql = 'select a.uid as panel_table_uid, b.panel_id, c.flawClassType as flaw_class_type, c.cameraId as station_id, c.station as station_name ' +
    'from doff a join panel_info b on a.uid = b.doff_id join flaw c on a.uid = c.doffId  where ' + query;
    let res = await sequelize_source.query(sql, {type: sequelize_source.QueryTypes.SELECT});
    return res;
  }
  /**
   * 返回缺陷类型的JSON
   * @param {*} sequelize_source
   * @param {*} uid
   * @returns
   */
  FlawFilterPiantie.getFlawFilterInfoByDoffUid = async (sequelize_source, queryStr) => {
      let query = typeof(queryStr) == 'string' ? `b.panel_id = "${queryStr}"` : `a.uid = ${queryStr}`;
      let sql = 'select a.detectParam from doff a inner join panel_info b on a.uid = b.doff_id where ' + query;
      let res = await sequelize_source.query(sql, {type: sequelize_source.QueryTypes.SELECT});
      return res.length ? res[0].detectParam : null;
  }
 /**
  *上传到产线flaw_filter 表
  * @param {Object} data 转换后上传的数据对象
  */
  FlawFilterPiantie.uploadPolInfo = async (data) => {
     let res = await FlawFilterPiantie.bulkCreate([...data], {
        updateOnDuplicate: ['flaw_class_type', 'show', 'flaw_count', 'symbol', 'color']
     });
     return res;
 }

 module.exports = {
    initFlawFilterData,
    FlawFilterPiantie
 };